package com.fone.player.online.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.BaseFragment;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.TopspotRst;
import com.fone.player.online.adapter.LeaderboardColumnAdapter;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.widget.xlistview.XListView;
import com.fone.player.widget.xlistview.XListView.IXListViewListener;


/**
 * VIP影院标签页。
 */
public class LeaderboardFragment extends BaseFragment implements IXListViewListener {
	/** 日志标签 */
	private static final String TAG = "LeaderboardFragment";

	private View mView;

	private static final int SUCCESS = 0;

	private boolean isFirst = true;

	private View empty;
	private View waitting;
	private View waittingProgress;
	
	private XListView mListView;
	private LeaderboardColumnAdapter mAdapter;
	
	private String shost;
	private String host;
	private String url;
	
	
	private void ensureContent() {
		if (mListView != null && waitting != null && waittingProgress!=null) {
			return;
		}
		View root = getView();
		if (root == null) {
			throw new IllegalStateException("Content view not yet created");
		}
		waitting = root.findViewById(R.id.leaderboard_waitting_ll);
		if (waitting == null) {
			throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.progress_container'");
		}
		waittingProgress = waitting.findViewById(R.id.leaderboard_progress);
		
		empty = root.findViewById(R.id.empty);
		if (empty != null) {
			empty.setVisibility(View.GONE);
		}
	}
	

	public static LeaderboardFragment newInstance(String url, String id) {
		LeaderboardFragment f = new LeaderboardFragment();

		// Supply String input as an argument.
		Bundle args = new Bundle();
		args.putString("url", url);
		args.putString("id", id);
		f.setArguments(args);
		return f;
	}

	public String getShownUrl() {
		return getArguments().getString("url");
	}

	public String getShownId() {
		return getArguments().getString("id");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		L.v(TAG, "---- onCreateView ----");

		mView = inflater.inflate(R.layout.fragment_leaderboard_column, container, false);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		L.v(TAG, "---- onActivityCreated ----");
		iniView();
	}

	@Override
	public void onStart() {
		super.onStart();
		L.v(TAG, "---- onStart ----");
		if(isFirst){
			iniData();
			isFirst = false;
		}
	}
	
	private void iniView(){
		//body
		mListView = (XListView) mView.findViewById(R.leaderboard.leaderboard_column_list);
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(true);
		mListView.setEnableDragLoadMore(false);
		mListView.setXListViewListener(this);
		mAdapter = new LeaderboardColumnAdapter(getActivity(), shost, host);
		mListView.setAdapter(mAdapter, true);
		
		empty = mView.findViewById(R.id.empty);
		
		waitting = mView.findViewById(R.id.leaderboard_waitting_ll);
		waittingProgress = waitting.findViewById(R.id.leaderboard_progress);
	}
	
	private void iniData(){
		//TODO  初始化数据  请求网络数据
		
		url = getShownUrl();
//		url="[host]/player/specllist?tmpid=15&ifp=0&clid=52503";
//		url="[host]/player/topspot?clid=52502&actionpath=30%7C0";
		
		showWaitting(true);
		Request.getInstance().topspot(FoneUtil.onlineUrl(url), new TopspotCallBack());
		L.v(TAG, "iniData", FoneUtil.onlineUrl(url));
		
//		fillData();
	}

	/**
	 * 填充数据
	 */
	private void fillData(TopspotRst t){
		
		mListView.stopRefresh();
		mListView.stopLoadMore();

		//TODO 填充数据
		shost = t.shost;
		host = t.host;

		if(t.modules!=null){
			mAdapter.initData(t.modules.moduleList,shost,host);
			mAdapter.notifyDataSetChanged();
		}
		showWaitting(false);
		
	}
	
	/**
	 * 是否显示waitting
	 * @param showing
	 */
	private void showWaitting(boolean showing){
		ensureContent();
		if(showing){
			waitting.setVisibility(View.VISIBLE);
			waittingProgress.setVisibility(View.VISIBLE);
		}else{
			waittingProgress.setVisibility(View.GONE);
			waitting.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onRefresh() {
		mListView.onLoad();
		
		//TODO 请求数据刷新
		ensureContent();
		waitting.setVisibility(View.VISIBLE);
		Request.getInstance().topspot(FoneUtil.onlineUrl(url), new TopspotCallBack());
	}
	
	@Override
	public void onLoadMore() {}

	
	private class TopspotCallBack implements Callback<TopspotRst> {

		@Override
		public void onSuccess(TopspotRst t) {
			L.v(TAG, "onSuccess", "result : " + t.result);
			if(t.result!=SUCCESS){
				mListView.stopRefresh();
				mListView.stopLoadMore();
				showWaitting(false);
				FoneUtil.showToast(ApplicationManage.getGlobalContext(), "数据获取失败，请稍后重试");
				return;
			}
			fillData(t);
		}

		@Override
		public void onFailure(Error error) {
			mListView.stopRefresh();
			mListView.stopLoadMore();
			showWaitting(false);
			L.v(TAG, "onFailure", "error : " + error.toString());
			if(error.isNetworkError()){
				FoneUtil.showToast(ApplicationManage.getGlobalContext(), "网络超时，请稍后重试");
				return;
			}
				FoneUtil.showToast(ApplicationManage.getGlobalContext(), "数据获取失败，请稍后重试");
		}
		
	}
	
	
	

	/**
	 * Detach from view.
	 */
	@Override
	public void onDestroyView() {
		isFirst = true;
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println(TAG + "---- onDestroy ----");
	}

}
